package org.hadoop.zk;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
public class WriteData {
    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("server101:2181", 30000, (e) -> {
            if (e.getState() == Watcher.Event.KeeperState.SyncConnected) {
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        Stat exists = zooKeeper.exists("/test", false);
        if (exists != null) {
            //最多可以保存1M的数据，-1将会自动的增长
            Stat stat = zooKeeper.setData("/test", "This Is New Data".getBytes(), -1);
            System.out.println("数据版本号：" + stat.getVersion());
        }
        zooKeeper.close();
    }
}
